import {message} from 'antd'
import {queryCashList, queryCashCheck, queryUnCashCheck} from '../services/api';

export default {
    namespace: 'cash',

    state: {
        data: {list: [], pagination: {size: 10, current: 1, total: 0}},
        cashModal: false,
        loading: false,
        confirmLoading:false,
    },

    effects: {
        *fetch({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryCashList, payload);
            yield put({
                type: 'save',
                payload: {pagination: response.page, list: response.data},
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        *check({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryCashCheck, payload);
            if (response.status) {
                message.success('审核成功');
                if (callback) {
                    callback();
                }
            } else {
                message.error('审核失败');
            }

            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        *uncheck({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryUnCashCheck, payload);
            if (response.status) {
                message.success('驳回成功');
                if (callback) {
                    callback();
                }
            } else {
                message.error('审核失败');
            }

            yield put({
                type: 'changeLoading',
                payload: false,
            });
        }
    },

    reducers: {
        save(state, {payload}) {
            return {
                ...state,
                data: payload,
            };
        },
        changeModal(state, {payload}) {
            return {
                ...state,
                cashModal: payload,
            };
        },
        changeLoading(state, {payload}) {
            return {
                ...state,
                loading: payload,
            };
        },
    },
};
